package com.antler.luajavalibrary.luaext.utils;

import android.app.Instrumentation;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.InputDevice;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;

import com.antler.luajavalibrary.application.MyApplication;

public class RuntimeUtils {
    private static final String TAG = "antler_" + RuntimeUtils.class.getSimpleName();
    private static Instrumentation sInstrumentation = null;

    private RuntimeUtils() {
        throw new AssertionError();
    }

    private static Instrumentation getInstrumentation() {
        if (sInstrumentation == null) {
            sInstrumentation = new Instrumentation();
        }
        return sInstrumentation;
    }

    public static PackageInfo getPackageInfo(String packageName) {
        Context context = MyApplication.getContext();

        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return packageInfo;
    }

    public static void launchApp(String packageName, String componentName) {
        Context context = MyApplication.getContext();

        if (getPackageInfo(packageName) != null) {
            PackageManager packageManager = context.getPackageManager();

            Intent intent = null;
            if (componentName == null || componentName.isEmpty()) {
                intent = packageManager.getLaunchIntentForPackage(packageName);
            } else {
                intent = new Intent();
                ComponentName component = new ComponentName(packageName, componentName);
                intent.setComponent(component);
            }

            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }

    public static void inputText(String inputText) {
        if (inputText == null) {
            return;
        }

        StringBuffer buff = new StringBuffer(inputText);

        boolean escapeFlag = false;
        for (int i = 0; i < buff.length(); i++) {
            if (escapeFlag) {
                escapeFlag = false;
                if (buff.charAt(i) == 's') {
                    buff.setCharAt(i, ' ');
                    buff.deleteCharAt(--i);
                }
            }
            if (buff.charAt(i) == '%') {
                escapeFlag = true;
            }
        }

        char[] chars = buff.toString().toCharArray();

        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        KeyEvent[] events = kcm.getEvents(chars);
        if (events != null) {
            for (int i = 0; i < events.length; i++) {
                getInstrumentation().sendKeySync(events[i]);
            }
        }
    }
}